#endcoding:utf-8
class SessionsController < ApplicationController
  def new
    if !session[:user_id].nil?
      redirect_to User.find(session[:user_id])
    end
  end
  def create
    @ut = params[:session][:user_type]
    member = User.find_by(email: params[:session][:email].downcase)
    if !member
      flash[:danger]="用户名不存在"
      render "new"
    else
      if member.user_type.to_s != params[:session][:user_type].to_s
        flash[:danger] = "用户类型错误"
        render "new"
      else
        if member && member.authenticate(params[:session][:password])
          if member.activated
            log_in member
            flash[:success] = "欢迎来到验厂服务网!"
            redirect_back_or(root_url)
          else
            flash[:danger] = "此账号未激活，请登录邮箱激活"
            render "new"
          end
        else
          flash[:danger] = "用户名或者密码错误"
          render "new"
        end
      end

    end
  end

  def destroy
    session.delete(:user_id)
    redirect_to welcome_index_path
  end
  private

end
